package server;

import java.util.Vector;
import java.util.Random;
import beans.Player_Bean;

/**********************************************************************
*
* Filename: Draft_Data.java
*
* File Description:
* Class to hold all of a drafts information
*
* Author:
* @author Tim Schmalz
* Date Created:
*  Oct 10, 2009
*
* Modified:
* @author Brandon Fulco
* Date Modified:
*  Oct 25, 2009
**********************************************************************/
public class Draft_Data
{
        private Vector<Client_Handler> draftList = new Vector<Client_Handler>();
        private Vector<Player_Bean> players;
        private int size = 0;
	private int currentDrafter = 0;
	private int inc = 1;   

        /**
         * Set the list of players
         *
         * @param players - Vecter of player beans
         */
        public void setPlayersList(Vector<Player_Bean> players)
        {
            this.players = players;
        }

        /**
         * Returns a vector of player beans
         *
         * @return players
         */
        public Vector<Player_Bean> getPlayersList()
        {
            return players;
        }

        /**
         * Add a team to the data
         *
         * @param newTeam
         */
	public void addNewTeam(Client_Handler newTeam)
	{
		draftList.add(newTeam);
                size++;
                randomizeDraftOrder();
	}

        /**
         * randomize the draft order
         *
         */
	public void randomizeDraftOrder()
	{
		Random r = new Random();
                Client_Handler temp;
		//Swaps 2*size times
		for(int i = 0; i < 2*size; i++)
		{
			//Get to random positions
			int swap1 = r.nextInt(size);
			int swap2 = r.nextInt(size);
                        System.out.println("Random: " + swap1 + " " + swap2);
			//Swap two of the current players in the list
			temp = draftList.get(swap1);
			draftList.set(swap1, draftList.get(swap2));
			draftList.set(swap2,temp);
		}
	}

        /**
         * Gets the whos turn it is
         *
         * @return Returns the team ids of the next drafter current drafter
         */
	public Drafters_Wrapper getCurrentDrafter()
	{
            Drafters_Wrapper nextDrafters = new Drafters_Wrapper();
            int size = draftList.size();
            int onDeckInc;
            int onDeck;
            final int MAX_TEAM_SIZE = 10;
            Database_Service db = new Database_Service();
            db.connectToDatabase();

            
            nextDrafters.setOnClockId(draftList.elementAt(currentDrafter).teamId);
            //Grab OnDeck
            if(currentDrafter == size-1 && inc == 1)
            {
                onDeckInc = -1;
                if ( db.getSizeRoster(draftList.elementAt(currentDrafter).teamId)+1 >= MAX_TEAM_SIZE )
                {
                    onDeck = -1;
                    nextDrafters.setOnDeckId(onDeck);
                }
                else
                {
                    onDeck = currentDrafter;
                    nextDrafters.setOnDeckId(draftList.elementAt(onDeck).teamId);
                }
            }
            else if(currentDrafter == 0 && inc == -1)
            {
                onDeckInc = 1;
                if ( db.getSizeRoster(draftList.elementAt(currentDrafter).teamId)+1 >= MAX_TEAM_SIZE )
                {
                    onDeck = -1;
                    nextDrafters.setOnDeckId(onDeck);
                }
                else
                {
                    onDeck = currentDrafter;
                    nextDrafters.setOnDeckId(draftList.elementAt(onDeck).teamId);
                }
            }
            else
            {
                onDeckInc = inc;
                if ( db.getSizeRoster(draftList.elementAt(currentDrafter+inc).teamId) >= MAX_TEAM_SIZE )
                {
                    onDeck = -1;
                    nextDrafters.setOnDeckId(-1);
                }
                else
                {
                    onDeck = currentDrafter+inc;
                    nextDrafters.setOnDeckId(draftList.elementAt(onDeck).teamId);
                }
            }
            //Grab InTheHole
            if(onDeck == -1)
            {
                nextDrafters.setInTheHoleId(-1);
            }
            else if(onDeck == size-1 && onDeckInc == 1)
            {
                if ( db.getSizeRoster(draftList.elementAt(onDeck).teamId)+1 >= MAX_TEAM_SIZE )
                {
                    nextDrafters.setInTheHoleId(-1);
                }
                else
                {
                    nextDrafters.setInTheHoleId(draftList.elementAt(onDeck).teamId);
                }
            }
            else if(onDeck == 0 && onDeckInc == -1)
            {
                if ( db.getSizeRoster(draftList.elementAt(onDeck).teamId)+1 >= MAX_TEAM_SIZE )
                {
                    nextDrafters.setInTheHoleId(-1);
                }
                else
                {
                    nextDrafters.setInTheHoleId(draftList.elementAt(onDeck).teamId);
                }
            }
            else
            {
                if ( db.getSizeRoster(draftList.elementAt(onDeck+onDeckInc).teamId) >= MAX_TEAM_SIZE )
                {
                    nextDrafters.setInTheHoleId(-1);
                }
                else
                {
                    nextDrafters.setInTheHoleId(draftList.elementAt(onDeck+onDeckInc).teamId);
                }
            }
            db.closeConnection();
            return nextDrafters;
	}

         /**
         * Incremetns the draft turn and returns the current drafter
         *
         * @return The current drafter
         */
        public Drafters_Wrapper incrementDrafter()
        {
                int size = draftList.size();

                if(currentDrafter == size-1 && inc == 1)
                {
                        inc = -1;
                        return getCurrentDrafter();
                }
                else if(currentDrafter == 0 && inc == -1)
                {
                        inc = 1;
                        return getCurrentDrafter();
                }
                else
                {
                        currentDrafter+=inc;
                        return getCurrentDrafter();
                }
        }

        /**
         * Gets a list of drafter's client handlers
         *
         * @return Vector of drafters
         */
        public Vector<Client_Handler> getDraftList()
        {
                return draftList;
        }

        /**
         * Checks to see if the drafter is currently logged in
         *
         * @param teamId - Team to be checked for
         * @return is the drafter currently in the draft list
         */
        public boolean isDrafterPresent(int teamId)
        {
            boolean isPresent = false;
            for(int i = 0; i < draftList.size(); i++)
            {
                if(draftList.get(i).getTeamId() == teamId)
                {
                    isPresent = true;
                    break;
                }
            }
            return isPresent;
        }
}
